/**
 * 
 */
package com.tomagency.cyberlab.client.mapper;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.tomagency.cyberlab.client.ClientFactory;
import com.tomagency.cyberlab.client.activity.BelegListenActivity;
import com.tomagency.cyberlab.client.activity.KontaktListenActivity;
import com.tomagency.cyberlab.client.activity.MessageListenActivity;
import com.tomagency.cyberlab.client.activity.ScanAppActivity;
import com.tomagency.cyberlab.client.events.WindowScrollEvent;
import com.tomagency.cyberlab.client.events.WindowScrollEventHandler;

import com.tomagency.cyberlab.client.place.ObjectListenPlace;
import com.tomagency.cyberlab.client.place.ScanAppPlace;

/**
 * @author gfr
 *
 */
public class AppActivityMapper implements ActivityMapper {
	
	private ClientFactory clientFactory;
	private WindowScrollEventHandler windowScrollEventHandlerDelegate;

	/**
	 * AppActivityMapper associates each Place with its corresponding
	 * {@link Activity}
	 * 
	 * @param clientFactory
	 *            Factory to be passed to activities
	 */
	public AppActivityMapper(ClientFactory clientFactory) {
		super();
		this.clientFactory = clientFactory;
		/*
		 * WindowScrollEventHandler für die Delegates (== aktive Activity) registrieren
		 * damit die laufende Activity auf WindowScrollEvents reagieren lkann
		 */
		clientFactory.getEventBus().addHandler(WindowScrollEvent.TYPE, new WindowScrollEventHandler()
		{

			@Override
			public void onWindowScrollEvent(WindowScrollEvent event) {
				if (windowScrollEventHandlerDelegate!=null){
					windowScrollEventHandlerDelegate.onWindowScrollEvent(event);
				}
			}

		});
	}
	
	BelegListenActivity belegListenActivity=null;
	KontaktListenActivity kontaktListenActivity=null;
	MessageListenActivity messageListenActivity=null;

	/**
	 * Map each Place to its corresponding Activity. This would be a great use
	 * for GIN.
	 */
	@Override
	public Activity getActivity(Place place) {
		if (place instanceof ObjectListenPlace){
			ObjectListenPlace op = (ObjectListenPlace)place;
			if (ObjectListenPlace.BELEGLISTE.equalsIgnoreCase(op.getPlaceToken())){
				belegListenActivity = belegListenActivity!=null ? belegListenActivity : new BelegListenActivity((ObjectListenPlace) place,clientFactory.getBelegListenView(), clientFactory);
				windowScrollEventHandlerDelegate = belegListenActivity;
				return belegListenActivity;				
			}
			else if (ObjectListenPlace.KONTAKTLISTE.equalsIgnoreCase(op.getPlaceToken())){
				
				kontaktListenActivity = kontaktListenActivity!=null ? kontaktListenActivity : new KontaktListenActivity((ObjectListenPlace) place,clientFactory.getKontaktListenView(), clientFactory);
				windowScrollEventHandlerDelegate = kontaktListenActivity;
				return kontaktListenActivity;		
			}
			else if (ObjectListenPlace.POSTINGLISTE.equalsIgnoreCase(op.getPlaceToken())){
				
				messageListenActivity = messageListenActivity!=null ? messageListenActivity : new MessageListenActivity((ObjectListenPlace) place,clientFactory.getMessageListenView(), clientFactory);
				windowScrollEventHandlerDelegate = messageListenActivity;
				return messageListenActivity;		
			}
			
		}
		else if (place instanceof ScanAppPlace){
			return new ScanAppActivity(clientFactory);
		}		
	
		return null;
	}

}
